/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.TheOnlyWayDown
{
	[CustomBehaviorFileName(@"25614-TheOnlyWayDown")]
	public class TheOnlyWayDown : CustomForcedBehavior
	{
		public TheOnlyWayDown(Dictionary<string, string> args)
			: base(args)
		{
		try
			{
				QuestId = 25614; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: The Only Way Down is in a Body Bag");
			}
		}

		public int QuestId { get; set; }
		public static LocalPlayer me = StyxWoW.Me;
		static public bool InVehicle { get { return Lua.GetReturnVal<int>("if IsPossessBarVisible() or UnitInVehicle('player') then return 1 else return 0 end", 0) == 1; } }
		public double angle = 0;
		public double CurentAngle = 0;
		
		public List<WoWUnit> mob1List
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => ((u.Entry == 34832 || u.Entry == 40942) && u.IsAlive && (u.X < 1619 && u.X > 1328) && (u.Y > -420 && u.Y < -280) && u.Z >= 130))
									.OrderBy(u => u.Distance).ToList();
			}
		}
		
		public List<WoWUnit> Turret
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>()
									.Where(u => (u.Entry == 32254))
									.OrderBy(u => u.Distance).ToList();
			}
		}
		
		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
		
		private Composite _root;
		protected override Composite CreateBehavior()
		{
			return _root ?? (_root =
				new PrioritySelector(
					
					
					new Decorator(ret => IsQuestComplete(),
						new Sequence(
							new Action(ret => TreeRoot.StatusText = "Finished!"),
							new Action(ret => Lua.DoString("VehicleExit()")),
							new WaitContinue(120,
							new Action(delegate
							{
								_isDone = true;
								return RunStatus.Success;
								}))
							)),
					new Decorator(ret => !InVehicle,
						new Action(ret =>
						{
							if (Turret.Count > 0 && Turret[0].Location.Distance(me.Location) <= 5)
							{
								WoWMovement.MoveStop();
								Turret[0].Interact();
							}
							else if (Turret.Count > 0 && Turret[0].Location.Distance(me.Location) > 5)
							{
								Navigator.MoveTo(Turret[0].Location);
							}
						}
					)),		
					new Action(ret =>
					{
						Lua.DoString("CastPetAction({0})", 1);	
						if(mob1List.Count == 0)
							return;
						mob1List[0].Target();
							
						while (me.CurrentTarget != null && me.CurrentTarget.IsAlive)
						{
							WoWMovement.ConstantFace(me.CurrentTarget.Guid);
							angle = (me.CurrentTarget.Z - me.Z) / (me.CurrentTarget.Location.Distance(me.Location));
							CurentAngle = Lua.GetReturnVal<double>("return VehicleAimGetAngle()", 0);
							if (CurentAngle < angle)
							{
								Lua.DoString(string.Format("VehicleAimIncrement(\"{0}\")", (angle - CurentAngle)));
							}
							if (CurentAngle > angle)
							{
								Lua.DoString(string.Format("VehicleAimDecrement(\"{0}\")", (CurentAngle - angle)));
							}
							Lua.DoString("CastPetAction({0})", 1);
						}
					}
					)
				)
			);
		}

		private bool _isDone;
		public override bool IsDone
		{
			get { return _isDone; }
		}
	}
}

